﻿using System.Collections;
using System.Collections.Generic;
using System.Xml;
using UnityEngine;
using UnityEngine.UI;

public class BattleData : MonoBehaviour
{
    public GameObject[] monster;
    public GameObject[] player = new GameObject[3];
    public Transform monsterPos;//怪物坐标
    //怪物
    public GameObject monster1;
    public GameObject monster2;
    public GameObject monster3;
    public GameObject monster4;
    public GameObject monster5;
    GameObject enemy;
    Vector3 position = new Vector3(0, 0, 0);
    int enemyNumber=4;//怪物数量
    GameObject canvas;//画布UI控制脚本
    public GameObject promptRound;//回合提示面板
    Text promptText;//回合显示文本
    GameObject game;
    public int goldCoin;//金币
    public int experience;//经验值

    //获取能量点
    public Text subject;
    Text optionA;
    Text optionB;
    Text optionC;
    Text optionD;
    Toggle optionToggleA;
    Toggle optionToggleB;
    Toggle optionToggleC;
    Toggle optionToggleD;
    int subjectNumber;//题目下标
    int subjectEnd;
    int subjectStart;
    int subscript = 0;
    int sequence = 1;//题目序列号
    string catalog;//练习题章节
    int combatLevel;
    int id;//题目数量最大值
    int number;//题目数量        
    //创建xml文档
    XmlDocument xml = new XmlDocument();

    // Start is called before the first frame update
    void Start()
    {
        game = GameObject.FindGameObjectWithTag("Data");
        combatLevel = game.GetComponent<GameData>().combatLevel;
        switch (combatLevel)
        {
            case 0: catalog = "item"; break; 
            case 1: catalog = "linear"; break;
            case 2: catalog = "stacksQueues"; break;
            case 3: catalog = "strand"; break;
            case 4: catalog = "array"; break;
            case 5: catalog = "tree"; break;
            case 6: catalog = "chart"; break;
        }
        xml.Load(Application.streamingAssetsPath + "/" + catalog + ".xml");
        XmlNodeList node = xml.SelectSingleNode(catalog).ChildNodes;
        foreach (XmlElement ele in node)
        {
            
            foreach (XmlElement i1 in ele.ChildNodes)
            {
                if (i1 .Name =="id")
                {
                    id = int.Parse(i1.InnerText); 
                }
            }
            
        }
        DataInitialization();
        canvas = GameObject.FindGameObjectWithTag("Canvas");
        monster = new GameObject[enemyNumber];
        Generate(enemy, enemyNumber);
        promptText = promptRound.transform.GetChild(0).GetComponent<Text>();
        Prompt("我方回合");
        TestLabel();
    }
    //对应该开关开启，能量值数值计算
    void OnToggleClick(Toggle toggle, bool value)
    {
        int i;
        for (i=0;i<subject .transform .childCount;i++)
        {
            if (subscript - 1 == i)
            {
                subject.transform.GetChild(i).GetChild(1).gameObject.SetActive(true);
            }
        }
        for (int j=0;j<subject .transform .childCount;j++)
        {
            if (subject .transform .GetChild (j).GetComponent <Toggle >().isOn ==true )
            {
                print(j);
                print(subscript);
                if (j== subscript-1)
                {
                    canvas.GetComponent<Control>().EnergyUpdata(5);
                }
            }
        }
        StartCoroutine("SubjectPanelEnd");
    }

    

    IEnumerator SubjectPanelEnd()
    {
        yield return new WaitForSeconds(1);
        subject.transform.GetChild(subscript).GetChild(1).gameObject.SetActive(false);
        subject.transform.GetChild(subscript).GetComponent<Toggle>().isOn = false;
        subject.transform.parent.gameObject.SetActive(false);
        StopCoroutine("SubjectPanelEnd");
    }

    void TestLabel()
    {
        optionToggleA = subject.transform.GetChild(0).GetComponent<Toggle>();
        optionToggleB = subject.transform.GetChild(1).GetComponent<Toggle>();
        optionToggleC = subject.transform.GetChild(2).GetComponent<Toggle>();
        optionToggleD = subject.transform.GetChild(3).GetComponent<Toggle>();
        optionA = optionToggleA.transform.GetChild(0).GetComponent<Text>();
        optionB = optionToggleB.transform.GetChild(0).GetComponent<Text>();
        optionC = optionToggleC.transform.GetChild(0).GetComponent<Text>();
        optionD = optionToggleD.transform.GetChild(0).GetComponent<Text>();
        optionToggleA.onValueChanged.AddListener((bool value) => OnToggleClick(optionToggleA, value));
        optionToggleB.onValueChanged.AddListener((bool value) => OnToggleClick(optionToggleB, value));
        optionToggleC.onValueChanged.AddListener((bool value) => OnToggleClick(optionToggleC, value));
        optionToggleD.onValueChanged.AddListener((bool value) => OnToggleClick(optionToggleD, value));

        switch (game .GetComponent <GameData >().subjectNumber)
        {
            case 0: subjectNumber = 0; subjectEnd = 4; break;
            case 1: subjectNumber = 5; subjectEnd = 12; break;
            case 2: subjectNumber = 13; subjectEnd = 22; break;
            case 3: subjectNumber = 23; subjectEnd = 28; break;
            case 4: subjectNumber = 29; subjectEnd = 34; break;
            case 5: subjectNumber = 35; subjectEnd = 40; break;
            case 6: subjectNumber = 41; subjectEnd = 46; break;
        }
    }

    void DataInitialization()
    {
        int i = game.GetComponent<GameData>().combatLevel;
        switch (i)
        {
            case 0: enemyNumber = 3; enemy = monster1; break;
            case 1: enemyNumber = 4; enemy = monster2; break;
            case 2: enemyNumber = 4; enemy = monster3; break;
            case 3: enemyNumber = 5; enemy = monster4; break;
            case 4: enemyNumber = 5; enemy = monster5; break;
            case 5: enemyNumber = 6; enemy = monster5; break;
            case 6: enemyNumber = 6; enemy = monster5; break;

        }
    }

    //奖励计算函数
    public void Reward()
    {
        int i = game.GetComponent<GameData>().combatLevel;
        switch (i)
        {
            case 0: goldCoin = 100; experience = 100; break;
            case 1: goldCoin = 150; experience = 150; break;
            case 2: goldCoin = 200; experience = 200; break;
            case 3: goldCoin = 250; experience = 250; break;
            case 4: goldCoin = 300; experience = 300; break;
            case 5: goldCoin = 350; experience = 350; break;
            case 6: goldCoin = 400; experience = 400; break;
            case 7: goldCoin = 450; experience = 450; break;

        }
        game.GetComponent<GameData>().goldCoin = goldCoin;
        game.GetComponent<GameData>().experience = experience;

    }

    void PlayerArray()
    {
        player[0] = canvas.GetComponent<Control>().role1;
        player[1] = canvas.GetComponent<Control>().role2;
        player[2] = canvas.GetComponent<Control>().role3;

    }

    //生成怪物
    public void Generate(GameObject monster1,int number)
    {
        for (int i=0;i<number;i++)
        {
            monster[i] = Instantiate(monster1);
            monster[i].transform.position = monsterPos.GetChild(i).transform.position;
            monster[i].transform.parent = transform;
            //enemy.transform.parent = transform;
            //monster[i] = enemy;
        }
    }

    //怪物进行攻击
    public void EnemyAttack()
    {
        PlayerArray();
        Prompt("敌方回合");
        StartCoroutine("EnemyStation");
    }

    //回合提示面板开关
    void Prompt(string name)
    {
        promptText.text = name;
        promptRound.SetActive(true);
        StartCoroutine("ClosePrompt");
    }

    IEnumerator ClosePrompt()
    {
        yield return new WaitForSeconds(1);
        promptRound.SetActive(false);
        StopCoroutine("ClosePrompt");
    }

    IEnumerator EnemyStation()
    {
        for (int i=0;i<monster .Length;i++)
        {
            if (monster [i]!=null)
            {
                Animator enemyAtor = monster[i].GetComponent<Animator>();
                yield return new WaitForSeconds(1);
                enemyAtor.SetBool("open", true);
                monster[i].GetComponent<EnemyZero>().DamageCalculation(player);
                yield return new WaitForSeconds(0.5f);
                enemyAtor.SetBool("open", false);
                yield return new WaitForSeconds(0.5f);
            }
        }
        StopCoroutine("EnemyStation");
        Prompt("我方回合");
        TopicsUpdata();
        canvas.GetComponent<Control>().PlayerRound();
    }

    void TopicsUpdata()
    {
        subject.transform.parent.gameObject.SetActive(true);
        for (int i = 0; i < subject.transform.childCount; i++)
        {
            subject.transform.GetChild(i).GetChild(1).gameObject.SetActive(false);
        }
        //subject.text = transform.GetComponent<ItemBank>().subject[subjectStart];
        //optionA.text = transform.GetComponent<ItemBank>().optionA[subjectStart];
        //optionB.text = transform.GetComponent<ItemBank>().optionB[subjectStart];
        //optionC.text = transform.GetComponent<ItemBank>().optionC[subjectStart];
        //optionD.text = transform.GetComponent<ItemBank>().optionD[subjectStart];
        //subjectStart++;
        //if (subjectStart >subjectEnd )
        //{
        //    subjectStart = subjectNumber;
        //}
        Exercises();
        if (number ==id)
        {
            sequence = 1;
        }
    }

    void Exercises()
    {
       
        //得到objects节点下的所有子节点
        XmlNodeList xmlNodeList = xml.SelectSingleNode(catalog).ChildNodes;
        //遍历所有子节点
        foreach (XmlElement xl1 in xmlNodeList)
        {

            if (xl1.Name ==catalog + sequence)
            {
                //继续遍历id为1的节点下的子节点
                foreach (XmlElement xl2 in xl1.ChildNodes)
                {
                    //得到name为a的节点里的内容。放到TextList里
                    if (xl2.Name == "id")
                    {
                        number = int.Parse(xl2.InnerText);
                    }
                    if (xl2.Name == "subject")
                    {
                        subject.text = xl2.InnerText;
                    }
                    if (xl2.Name == "optionA")
                    {
                        optionA.text = xl2.InnerText;
                    }
                    if (xl2.Name == "optionB")
                    {
                        optionB.text = xl2.InnerText;
                    }
                    if (xl2.Name == "optionC")
                    {
                        optionC.text = xl2.InnerText;
                    }
                    if (xl2.Name == "optionD")
                    {
                        optionD.text = xl2.InnerText;
                    }
                    if (xl2.Name == "answer")
                    {
                        subscript = int.Parse(xl2.InnerText);
                        print(subject);
                    }
                }

            }
        }
        sequence++;
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
